package com.demo.controller;


import com.demo.entity.Medicine;
import com.demo.entity.MessageRule;
import com.demo.response.Result;
import com.demo.service.IMessageRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 药物提醒规则配置表 前端控制器
 * @author ZM
 * @since 2020-11-23
 */
@RestController
@Api(tags = "药物提醒规则配置类")
@RequestMapping("/messageRule")
public class MessageRuleController {
    @Autowired
    IMessageRuleService ruleService;

    @ApiOperation("新增药物规则")
    @PostMapping("/addMessageRule")
    public Result addMessageRule(@Validated @RequestBody MessageRule messageRule, BindingResult result){
        if (result.hasErrors()){
            return Result.error(result.getFieldError().getDefaultMessage());
        }
        return ruleService.addMessageRule(messageRule);
    }

    @ApiOperation("编辑药物规则")
    @PostMapping("/updateMessageRule")
    public Result updateMessageRule(@RequestBody MessageRule messageRule, BindingResult result){
        if (result.hasErrors()){
            return Result.error(result.getFieldError().getDefaultMessage());
        }
        return ruleService.updateMessageRule(messageRule);
    }

    @ApiOperation("删除药物规则")
    @GetMapping("/deleteMessageRule")
    public Result deleteMessageRule(@RequestParam(name = "messageRuleId")Integer messageRuleId,
                                 @RequestParam(name = "deletedUserId")Integer deletedUserId){
        return ruleService.deleteMessageRule(messageRuleId, deletedUserId);
    }

    @ApiOperation("药物规则列表")
    @GetMapping("/messageRuleList")
    public Result messageRuleList(@RequestParam(name = "pageNumber",required = false,defaultValue = "1")Integer pageNumber,
                               @RequestParam(name = "pageSize",required = false,defaultValue = "10")Integer pageSize,
                               @RequestParam(name = "medicineId",required = false)Integer medicineId){
        return ruleService.messageRuleList(pageNumber, pageSize, medicineId);
    }

}
